package com.kit.web.controller.adv;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.kit.common.constant.APIUrlConstantsForMTX;
import com.kit.common.constant.CacheConstants;
import com.kit.common.core.controller.BaseController;
import com.kit.common.utils.CretUtil;
import com.kit.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;

/**
 * 内部工具请求控制器
 * @Description com.kit.web.controller.adv
 * @Author Kit
 * @Date 2024-05-16 14:32
 * @Version 1. 0
 **/

@RestController
@RequestMapping("/adv")
public class InteriorUtilController extends BaseController {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取/刷新满天星token数据
     * @author: Kit
     * @date: 2024/5/16 14:37
     * @param:
     * @return:
     **/
    @GetMapping("/interiorUtil/refreshMTXToken")
    public void refreshMTXToken() {
        //先获取满天星code码
        JSONObject jsonObject = new JSONObject();
        long timestamp = DateUtils.getTimestamp();
        jsonObject.putOnce("timestamp", timestamp);
        String res = HttpUtil.post(APIUrlConstantsForMTX.GET_CODE, jsonObject);
        JSONObject resObj = JSONUtil.parseObj(res);
        if (resObj.getInt("retCode") == 0 && "success".equals(resObj.getStr("result"))) {
            //根据CODE码获取Token,resObj.getStr("code")
            String password_sha = CretUtil.calculateSHA1(CretUtil.calculateMD5(APIUrlConstantsForMTX.MTX_SERVE_PWD)+APIUrlConstantsForMTX.MTX_SERVE_USERNAME);
            String password_comb = CretUtil.calculateMD5(password_sha+resObj.getStr("code"));
            String calculateMD5Str = APIUrlConstantsForMTX.MTX_SERVE_USERNAME + password_comb + resObj.getStr("code") + timestamp;
            // 计算MD5值
            String md5Hash = CretUtil.calculateMD5(calculateMD5Str);
            JSONObject tokenParam = new JSONObject();
            tokenParam.putOnce("username",APIUrlConstantsForMTX.MTX_SERVE_USERNAME);
            tokenParam.putOnce("password",password_comb);
            tokenParam.putOnce("timestamp",DateUtils.getTimestamp());
            tokenParam.putOnce("sign",md5Hash);
            tokenParam.putOnce("seccode",resObj.getStr("code"));
            String tokenResStr = HttpUtil.post(APIUrlConstantsForMTX.GET_TOKEN, tokenParam);
            JSONObject tokenResObj = JSONUtil.parseObj(tokenResStr);
            if (tokenResObj.getInt("retCode") == 0 && "OK".equals(tokenResObj.getStr("retInfo"))) {
                //存入redis
                redisTemplate.opsForValue().set(CacheConstants.MTX_ACCESS_TOKEN,tokenResObj.getStr("access_token"));
                redisTemplate.opsForValue().set(CacheConstants.MTX_ACCESS_SECRET,tokenResObj.getStr("access_secret"));
            }
        } else {
            logger.error("获取/刷新满天星token数据异常:" +  res);
        }
    }
}
